package cn.hzxs.zjn.studentmanagement.controller;

import cn.hzxs.zjn.studentmanagement.domain.Page;
import cn.hzxs.zjn.studentmanagement.domain.Plan;
import cn.hzxs.zjn.studentmanagement.sevice.PlanService;
import cn.hzxs.zjn.studentmanagement.sevice.UserService;
import cn.hzxs.zjn.studentmanagement.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Author: Zjn *Bring it on!!!*
 * @Date: 2021/3/28 14:32
 * Note:
 */
@RestController
public class PlanController {
    @Autowired
    PlanService planService;

    @GetMapping("/getPlan")     //获取所有人计划
    public JsonResult getPlan(@RequestParam(defaultValue = "0") Integer offset,
                              @RequestParam(defaultValue = "5") Integer limit) {
        List<Plan> plan = planService.getPlan(new Page(offset, limit));
        HashMap<Object, Object> hashMap = new HashMap<>();
        if (plan.size() > 0) {
            for (int i = 0; i < plan.size(); i++) {
                hashMap.put(i, plan.get(i));
            }
            return new JsonResult(hashMap);
        } else
            return new JsonResult(hashMap,"200","没有数据");
    }

    @PostMapping("/addPlan")
    public JsonResult addPlan(@RequestBody Plan plan) {
        HashMap<Object, Object> hashMap = new HashMap<>();
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        if (pattern.matcher(plan.getTYPE()).matches()) {
            hashMap.put("status", "ok");
            planService.addPlan(plan);
            return new JsonResult(hashMap);
        }
        hashMap.put("status", "error");
        return new JsonResult(hashMap, "200", "计划类型错误");
    }

    @PostMapping("/planGet")
    public JsonResult planGet(@RequestBody Plan plan) {
        List<Plan> planList = planService.getPlanByAuthorId(plan);
        HashMap<Object, Object> hashMap = new HashMap<>();
        for (int i = 0; i < planList.size(); i++) {
            hashMap.put(i, planList.get(i));
        }
        return new JsonResult(hashMap);
    }
}
